package com.future.cor.v1;

import java.util.ArrayList;
import java.util.List;

public class Servlet_main {

    public static void main(String[] args) {
        FilterChains chains = new FilterChains().addFilter(new Filter1()).addFilter(new Filter2());
        Request request = new Request();
        request.str = "www.你好呀，恭喜呗被company公司录用";
        Response response = new Response();
        response.str = "response";
        chains.doFilter(request, response);
        System.out.println(request.str);
        System.out.println(response.str);
    }
}

class Filter1 implements Filter {

    @Override
    public void doFilter(Request request, Response response, FilterChains filterChains) {
        request.str.replace("www", "future");
        filterChains.doFilter(request, response);
        response.str += "filter1";
    }
}

class Filter2 implements Filter {
    @Override
    public void doFilter(Request request, Response response, FilterChains filterChains) {
        request.str.replace("company", "baidu.com");
        filterChains.doFilter(request, response);
        response.str += "filter2";
    }
}

interface Filter {
    void doFilter(Request request, Response response, FilterChains filterChains);
}

class FilterChains {
    int index = 0;
    List<Filter> list = new ArrayList<>();

    public FilterChains addFilter(Filter filter) {
        list.add(filter);
        return this;
    }

    public void doFilter(Request request, Response response) {
        if (list.size() == index) {
            return;
        }
        Filter filter = list.get(index);
        index++;
        filter.doFilter(request, response, this);
    }
}

class Request {
    String str;
}

class Response {
    String str;
}
